<template>
  <div class="view-fill">
    <div class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :btnList="fullInfo.btnList"
        @returnBack="$emit('returnBack')"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('pendCheckProduct:update') &&
              formInfo.data.checkStatus === 1
            "
            :plain="true"
            @click="save"
            >保存</el-button
          >
          <el-button
            v-if="
              $permission('pendCheckProduct:confirm') &&
              ((formInfo.data.checkStatus === 4 &&
                formInfo.data.isCharge === 1) ||
                formInfo.data.checkStatus === 1)
            "
            :plain="true"
            @click="confirm"
            >确认</el-button
          >
          <el-button
            v-if="
              $permission('pendCheckProduct:audit') &&
              formInfo.data.checkStatus === 1 &&
              formInfo.data.isCharge === 1
            "
            :plain="true"
            @click="audit"
            >审批</el-button
          >
          <el-button
            v-if="
              $permission('pendCheckProduct:revoke') &&
              formInfo.data.checkStatus === 1 &&
              formInfo.data.isCharge === 1
            "
            :plain="true"
            @click="revoke"
            >撤回</el-button
          >
          <el-button
            v-if="
              $permission('pendCheckProduct:refuse') &&
              formInfo.data.checkStatus === 1
            "
            :plain="true"
            @click="refuse"
            >拒绝</el-button
          >
        </template>

        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <page-form
              class="paddingBox order-model-formBox flex_wrap_start_start"
              v-model:ref-obj="formInfo.ref"
              :data="formInfo.data"
              :field-list="formInfo.fieldList"
              :rules="formInfo.rules"
              :label-width="formInfo.labelWidth"
              :list-type-info="listTypeInfo"
            >
              <template v-slot:form-licenseNumber>
                <el-input
                  class="not-mongolia"
                  type="input"
                  :disabled="true"
                  placeholder="请选择证照"
                  v-model="formInfo.data.licenseNumber"
                >
                  <template #append>
                    <el-icon @click="openLicenseList"><MoreFilled /></el-icon>
                  </template>
                </el-input>
              </template>
              <template v-slot:form-g1Value>
                <el-input
                  class="not-mongolia"
                  type="input"
                  :disabled="true"
                  placeholder="请选择"
                  v-model="formInfo.data.g1Value"
                >
                  <template #append>
                    <el-icon @click="openBarCodeRules"><MoreFilled /></el-icon>
                  </template>
                </el-input>
              </template>
              <template v-slot:form-productCategoryId>
                <el-tooltip
                  effect="light"
                  :disabled="!formInfo.data.productCategoryValue"
                  :content="formInfo.data.productCategoryValue"
                  placement="top-start"
                >
                  <el-input
                    class="not-mongolia"
                    type="input"
                    :disabled="true"
                    placeholder="请选择分类"
                    v-model="formInfo.data.productCategoryValue"
                  >
                    <template #append>
                      <el-icon @click="openCategoryList"
                        ><MoreFilled
                      /></el-icon>
                    </template>
                  </el-input>
                </el-tooltip>
              </template>
            </page-form>
          </div>
        </div>
        <select-license
          v-if="selectLicenseInfo.visible"
          @callback="selectLicense"
          :saveEmpty="true"
          :query="selectLicenseInfo.query"
          v-model:showDialog="selectLicenseInfo.visible"
        />
        <SelectBarCodeRules
          v-if="selectBarCodeRules.visible"
          @callback="onSelectBarCodeRules"
          v-model:showDialog="selectBarCodeRules.visible"
          v-model:getCoreProductRule="selectBarCodeRules.rules"
        />
        <select-product-classification
          v-if="selectClassification.visible"
          :query="selectClassification.query"
          v-model:show-dialog="selectClassification.visible"
          :saveEmpty="selectClassification.saveEmpty"
          @callback="selectClassificationCallback"
        ></select-product-classification>
      </NewFullScreen>
    </div>
  </div>
</template>

<script setup name="productModify">
import {
  getBrandAll,
  getProductCheck,
  getProductCategoryTree,
  getUnitAll,
  updateProductCheck,
  getCoreProductRule,
  getCoreCodeStatus,
  getCoreSystemConfigAll,
  productCheckRefuse,
  productCheckAudit,
  productCheckRevoke,
} from '@/api/core'
import PageForm from '@/components/PageForm/index'

import SelectLicense from '@/components/SelectLicense/index'
import SelectBarCodeRules from '../../product/components/SelectBarCodeRules/index'
import SelectProductClassification from '@/components/SelectProductClassification/index'
import { ElMessage } from 'element-plus'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const props = defineProps(['visible', 'title', 'id', 'type', 'productId'])

const formInfo = reactive({
  ref: null,
  systemConfig: {},
  data: {
    code: '',
    needLicense: 1,
    expectedUsageTimes: 1,
    unitConvertCoefficient: 1,
    putShelfStatus: 1,
    productNearlyEffective:
      proxy.$store.state.permission.systemConfig.product_nearly_effective,
    qtyOperateMode: proxy.$store.state.permission.systemConfig.qty_operate_mode,
  },
  formDisabled: false,
  delFiledList: [],
  fieldList: [
    {
      label: '证号必填',
      value: 'needLicense',
      type: 'radio-model',
      code: 'needLicense_type',
      required: true,
      show: true,
    },
    {
      label: '证号',
      value: 'licenseNumber',
      type: 'slot',
      required: true,
    },
    {
      label: '证照名称',
      value: 'licenseName',
      type: 'input',
      disabled: true,
      placeholder: false,
    },
    {
      label: '英文名称',
      value: 'enName',
      type: 'input',
      disabled: true,
      placeholder: false,
    },
    {
      label: '证照生效日期',
      value: 'licenseBeginDate',
      type: 'input',
      disabled: true,
      placeholder: false,
    },
    {
      label: '证照失效日期',
      value: 'licenseEndDate',
      type: 'input',
      disabled: true,
      placeholder: false,
    },
    {
      label: '包装规格',
      value: 'packedStandard',
      type: 'input',
      disabled: true,
      placeholder: false,
    },
    {
      label: '生产厂家',
      value: 'brandName',
      type: 'input',
      disabled: true,
      placeholder: false,
    },
    {
      label: '生产厂家',
      value: 'brandId',
      type: 'select',
      show: false,
      disabled: true,
      list: 'brandList',
      filterable: true,
      clearable: true,
    },
    {
      label: '通用名称',
      value: 'commonName',
      type: 'input',
      required: true,
    },
    { label: '编码', value: 'code', type: 'input', show: true },
    { label: 'his编码', value: 'hisCode', type: 'input', show: true },
    { label: '别名', value: 'alias', type: 'input' },
    { label: '助记符', value: 'mnemonic', type: 'input' },
    {
      label: '产品类型',
      value: 'productType',
      type: 'select-model',
      clearable: true,
      code: 'new_product_type',
      labelTip:
        '普通记账耗材：医院直接向患者收费的项目<br/>普通不记账耗材：医院科室内部使用不直接向患者收费的项目',
    },
    {
      label: '产品规格',
      value: 'standard',
      type: 'input',
      required: true,
    },
    {
      label: '近效期（天）',
      value: 'productNearlyEffective',
      type: 'number',
      required: true,
    },
    { label: '包装规格', value: 'packingSpecification', type: 'input' },
    {
      label: '计量单位',
      value: 'unitId',
      type: 'select',
      required: true,
      list: 'unitList',
      filterable: true,
    },
    {
      label: '计算单位',
      value: 'calcUnitId',
      type: 'select',
      list: 'unitList',
      filterable: true,
      show: true,
      required: true,
    },
    {
      label: '数量操作模式',
      value: 'qtyOperateMode',
      show: true,
      type: 'select-model',
      clearable: true,
      code: 'qty_operate_mode',
      disabled: props.productId == -2 ? false : true,
    },
    {
      label: '单位换算系数',
      value: 'unitConvertCoefficient',
      type: 'number',
      min: 1,
      show: true,
      disabled: props.productId == -2 ? false : true,
    },
    {
      label: '状态',
      value: 'status',
      type: 'radio-model',
      code: 'new_enabled_type',
      required: true,
    },
    {
      label: '发票类别',
      value: 'invoiceCategory',
      show: true,
      type: 'select-model',
      clearable: true,
      code: 'invoice_category',
    },
    {
      label: '病案类别',
      value: 'caseCategory',
      show: true,
      type: 'select-model',
      clearable: true,
      code: 'case_category',
    },
    {
      label: '财务科目',
      value: 'financialAccount',
      show: true,
      type: 'select-model',
      clearable: true,
      code: 'financial_account',
      required: true,
    },
    {
      label: '条码类型',
      value: 'codeType',
      type: 'select-model',
      code: 'code_type',
      onlyShowOption: proxy.$fn.splitArr(
        proxy.$store.state.permission.systemConfig.code_type_support
      ),
      checkDefault: false,
      disabled: props.productId == -2 ? false : true,
    },
    { label: '原厂主码', value: 'mainCode', show: true, type: 'input' },
    {
      label: '需要批号效期',
      value: 'needBatchNumber',
      type: 'radio-model',
      code: 'default_yes_or_no',
      required: true,
    },
    {
      label: '需要序列管理',
      value: 'needSequence',
      type: 'radio-model',
      code: 'default_yes_or_no',
      required: true,
    },
    {
      label: '手术跟台',
      value: 'withOperateTable',
      type: 'radio-model',
      code: 'default_no_or_yes',
      required: true,
      labelTip: '医院0库存，使用后结算的产品',
      show: false,
    },
    {
      label: '以领定消',
      value: 'acquisitionReplaceConsume',
      type: 'radio-model',
      code: 'default_no_or_yes',
      required: true,
      labelTip: '科室领用即算消耗的产品',
    },
    {
      label: '防疫物资',
      value: 'epidemicPrevention',
      type: 'radio-model',
      code: 'default_no_or_yes',
      required: true,
    },
    {
      label: '临采产品',
      value: 'temporaryPurch',
      type: 'radio-model',
      code: 'default_no_or_yes',
      required: true,
    },
    {
      label: '是否议价',
      value: 'isBargaining',
      type: 'radio-model',
      code: 'default_no_or_yes',
    },
    {
      label: '是否挂网',
      value: 'isSunPurchase',
      type: 'radio-model',
      code: 'default_no_or_yes',
      labelTip: '挂网：阳光采购网',
    },
    {
      label: '挂网类型',
      value: 'networkType',
      show: false,
      type: 'select-model',
      clearable: true,
      code: 'network_type',
      checkDefault: true,
    },
    {
      label: '商品代码',
      value: 'serialNum',
      show: false,
      type: 'input',
    },
    {
      label: '是否收费',
      value: 'isCharge',
      type: 'radio-model',
      code: 'default_no_or_yes',
    },
    {
      label: '是否寄售',
      value: 'isConsignment',
      type: 'radio-model',
      code: 'default_no_or_yes',
      disabled:
        proxy.$store.state.permission.systemConfig.is_consign_business == '0'
          ? true
          : props.productId == -2
          ? false
          : true,
    },
    {
      label: '结算方式',
      value: 'settlementMethod',
      show: true,
      type: 'select-model',
      clearable: true,
      code: 'settlement_method',
      disabled: true,
    },

    {
      label: '限制价格',
      value: 'isLimitPrice',
      type: 'radio-model',
      code: 'default_no_or_yes',
    },
    {
      label: '采购限制价格',
      value: 'purchaseLimitPrice',
      type: 'number',
      max: 999999999999999,
      show: false,
      formatter: proxy.$fn.TripartiteMethodAsset,
      precision:
        proxy.$store.state.permission.systemConfig.assets_price_precision,
    },
    {
      label: '上架状态',
      value: 'putShelfStatus',
      type: 'radio-model',
      code: 'put_down_shelf',
    },
    {
      label: '厂家理论测试次数',
      value: 'expectedUsageTimes',
      type: 'number',
      min: 1,
      code: 'default_no_or_yes',
    },
    {
      label: '质管类型',
      value: 'dosageForm',
      type: 'select-model',
      clearable: true,
      checkDefault: false,
      code: 'new_dosage_form',
    },

    {
      label: '库存类别',
      value: 'inventoryCategories',
      type: 'select-model',
      code: 'inventory_categories',
      checkDefault: false,
      clearable: true,
    },
    { label: '产品分类', value: 'productCategoryId', type: 'slot' },
    { label: '国家医保编码', value: 'nationCode', type: 'input' },
    { label: '省医保编码', value: 'provinceCode', type: 'input' },
    {
      label: '国家医保名称',
      value: 'nationHealthInsurance',
      type: 'input',
    },
    { label: '原厂码规则', value: 'g1Value', type: 'slot', show: true },
    {
      label: '产品长度(cm)',
      value: 'productLength',
      type: 'number',
      precision: 2,
      min: 1,
    },
    {
      label: '产品宽度(cm)',
      value: 'productWidth',
      precision: 2,
      type: 'number',
      min: 1,
    },
    {
      label: '产品高度(cm)',
      value: 'productHeight',
      precision: 2,
      type: 'number',
      min: 1,
    },
    {
      label: '备注',
      value: 'remark',
      type: 'textarea',
    },
    {
      label: '创建人',
      value: 'createUserName',
      type: 'input',
      showDetail: true,
    },
    {
      label: '创建时间',
      value: 'createTime',
      type: 'input',
      showDetail: true,
    },
  ],
  rules: {
    mainCode: [
      {
        required: true,
        message: '请输入原厂主码',
        trigger: 'blur',
      },
    ],
    commonName: [
      { required: true, message: '请输入通用名称', trigger: 'blur' },
    ],
    standard: [{ required: true, message: '请输入规格', trigger: 'blur' }],
    unitId: [{ required: true, message: '请选择计量单位', trigger: 'change' }],
    calcUnitId: [
      { required: true, message: '请选择计算单位', trigger: 'change' },
    ],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    financialAccount: [
      { required: true, message: '请选择财务科目', trigger: 'change' },
    ],
    settlementMethod: [
      { required: true, message: '请选择结算方式', trigger: 'change' },
    ],
    purchaseLimitPrice: [
      {
        required: true,
        message: '请输入采购限制价格',
        trigger: 'change',
      },
    ],
    expectedUsageTimes: [
      {
        required: true,
        message: '请输入厂家理论测试次数',
        trigger: 'change',
      },
    ],
    networkType: [
      { required: true, message: '请选择挂网类型', trigger: 'change' },
    ],
    serialNum: [
      { required: true, message: '请输入商品代码', trigger: 'change' },
    ],
    qtyOperateMode: [
      {
        required: true,
        message: '请选择数量操作模式',
        trigger: 'change',
      },
    ],
    codeType: [
      { required: true, message: '请选择条码类型', trigger: 'change' },
    ],
    productType: [
      { required: true, message: '请选择产品类型', trigger: 'change' },
    ],
    licenseNumber: [
      { required: true, message: '请选择证号', trigger: 'change' },
    ],
    needBatchNumber: [
      {
        required: true,
        message: '请选择是否需要批号效期',
        trigger: 'change',
      },
    ],
    needSequence: [
      {
        required: true,
        message: '请选择是否需要序列管理',
        trigger: 'change',
      },
    ],
    code: [
      { required: true, message: '请输入编码', trigger: 'blur' },
      { validator: proxy.$validate.validatEn, trigger: 'blur' },
    ],
    productNearlyEffective: [
      {
        required: true,
        type: 'number',
        min: 1,
        message: '请输入产品近效期',
        trigger: 'blur',
      },
    ],
    unitConvertCoefficient: [
      {
        required: true,
        type: 'number',
        min: 1,
        message: '请输入转换系数，最小值为1',
        trigger: 'blur',
      },
    ],
  },
  labelWidth: '120px',
})
const selectLicenseInfo = reactive({
  visible: false,
  query: {
    status: '1',
  },
})
const selectClassification = reactive({
  visible: false,
  saveEmpty: true,
  query: {},
})
const selectBarCodeRules = reactive({
  visible: false,
  rules: undefined,
})
const listTypeInfo = reactive({
  licenseList: [],
  brandList: [],
  unitList: [],
})

const fullInfo = reactive({
  btnList: [],
})
const tag = ref(0)
const isFirstUpdate = ref(false)
const options = ref([])

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emit('update:visible', newValue) // 更新父组件visible
  },
})

watch(
  () => formInfo.data.codeType,
  (value) => {
    showFieldListItem(value == 3, 'g1Value')
    showFieldListItem(value == 3, 'mainCode')
    if (value == 3) {
      let type = { type: 'GS1' }
      ;(formInfo.data.g1 = type), (formInfo.data.g1Value = 'GS1规则')
      selectBarCodeRules.rules = type
    } else {
      ;(formInfo.data.g1 = {}), (formInfo.data.g1Value = '')
      selectBarCodeRules.rules = {}
    }
  }
)

watch(
  () => formInfo.data.needLicense,
  (value) => {
    if (value == 1) {
      formInfo.rules.licenseNumber[0].required = true
      disabledFieldListItem(true, 'brandName')
      disabledFieldListItem(true, 'brandId')
      showFieldListItem(true, 'brandName')
      showFieldListItem(false, 'brandId')
      if (!isFirstUpdate.value) {
        formInfo.data['packedStandard'] = ''
        formInfo.data['brandName'] = ''
        formInfo.data['brandId'] = ''
        formInfo.data['licenseTypeValue'] = ''
        formInfo.data['licenseName'] = ''
        formInfo.data['licenseBeginDate'] = ''
        formInfo.data['licenseEndDate'] = ''
        formInfo.data['enName'] = ''
        formInfo.data['licenseId'] = -2
        formInfo.data['commonName'] = ''
        formInfo.data['licenseNumber'] = ''
      }
      formInfo.data['brandCode'] = ''
    } else if (value == 0) {
      formInfo.rules.licenseNumber[0].required = false
      if (formInfo.data.licenseNumber) {
        disabledFieldListItem(true, 'brandName')
        disabledFieldListItem(true, 'brandId')
      } else {
        disabledFieldListItem(false, 'brandName')
        disabledFieldListItem(false, 'brandId')
      }
      showFieldListItem(false, 'brandName')
      showFieldListItem(true, 'brandId')
      if (!isFirstUpdate.value) {
        formInfo.data['packedStandard'] = ''
        formInfo.data['brandName'] = ''
        formInfo.data['brandId'] = ''
        formInfo.data['licenseTypeValue'] = ''
        formInfo.data['licenseName'] = ''
        formInfo.data['licenseBeginDate'] = ''
        formInfo.data['licenseEndDate'] = ''
        formInfo.data['enName'] = ''
        formInfo.data['licenseId'] = -2
        formInfo.data['commonName'] = ''
        formInfo.data['licenseNumber'] = ''
      }
      formInfo.data['brandCode'] = ''
      isFirstUpdate.value = false
    }
  }
)

watch(
  () => formInfo.data.isLimitPrice,
  (value) => {
    if (value == 1) {
      showFieldListItem(true, 'purchaseLimitPrice')
    } else {
      showFieldListItem(false, 'purchaseLimitPrice')
      formInfo.data['purchaseLimitPrice'] = 0
    }
  }
)
watch(
  () => formInfo.data.isSunPurchase,
  (value) => {
    if (value == 1) {
      showFieldListItem(true, 'networkType')
      showFieldListItem(true, 'serialNum')
      formInfo.data['isSunPurchaseValue'] = '是'
    } else {
      showFieldListItem(false, 'networkType')
      showFieldListItem(false, 'serialNum')

      formInfo.data['networkType'] = -2
      formInfo.data['serialNum'] = ''
      formInfo.data['isSunPurchaseValue'] = '否'
    }
  }
)

watch(
  () => formInfo.data.isBargaining,
  (value) => {
    if (value == 1) {
      formInfo.data['isBargainingValue'] = '是'
    } else {
      formInfo.data['isBargainingValue'] = '否'
    }
  }
)

watch(
  () => formInfo.data.isCharge,
  (value) => {
    if (value == 1) {
      formInfo.data['isChargeValue'] = '是'
    } else {
      formInfo.data['isChargeValue'] = '否'
    }
  }
)
watch(
  () => formInfo.data.isConsignment,
  (value) => {
    if (value == 1) {
      formInfo.data['isConsignmentValue'] = '是'
      formInfo.data['settlementMethod'] = 1
      formInfo.data['settlementMethodValue'] = '消耗后结算'
      showFieldListItem(true, 'withOperateTable')
    } else {
      formInfo.data['isConsignmentValue'] = '否'
      formInfo.data['settlementMethod'] = '2'
      formInfo.data['settlementMethodValue'] = '入库后结算'
      formInfo.data['withOperateTable'] = 0
      showFieldListItem(false, 'withOperateTable')
    }
  }
)
watch(
  () => formInfo.data.putShelfStatus,
  (value) => {
    if (value == 1) {
      formInfo.data['putShelfStatusValue'] = '是'
    } else {
      formInfo.data['putShelfStatusValue'] = '否'
    }
  }
)
watch(
  () => formInfo.data.qtyOperateMode,
  (value) => {
    showFieldListItem(value == 2, 'unitConvertCoefficient')
    showFieldListItem(value == 2, 'calcUnitId')
  }
)
onMounted(() => {
  getBrandList()
  getClassficationTree()
  getUnitList()
  getStatus()
  getCoreSystemConfig()
  if (props.id) {
    getDetails()
    isFirstUpdate.value = true
  }
})

function openCategoryList() {
  selectClassification.visible = true
}
function selectClassificationCallback(data) {
  if (data) {
    formInfo.data['productCategoryId'] = data.id
    if (formInfo.data.productCategoryId === -2) {
      formInfo.data['productCategoryValue'] = ''
    } else {
      formInfo.data['productCategoryValue'] = data.name
    }
  } else {
    formInfo.data['productCategoryValue'] = ''
    formInfo.data['productCategoryId'] = -2
  }
  selectClassification.visible = false
}
function showFieldListItem(show, nameValue) {
  let fieldList = formInfo.fieldList
  fieldList.forEach((item) => {
    if (nameValue === item.value) {
      item.show = show
    }
  })
}
function disabledFieldListItem(enable, nameValue) {
  let fieldList = formInfo.fieldList
  fieldList.forEach((item) => {
    if (nameValue === item.value) {
      item.disabled = enable
    }
  })
}
function showPlaceholderFieldListItem(enable, nameValue) {
  let fieldList = formInfo.fieldList
  fieldList.forEach((item) => {
    if (nameValue === item.value) {
      item.placeholder = enable === false ? enable : ''
    }
  })
}
function openLicenseList() {
  selectLicenseInfo.visible = true
}
function openBarCodeRules() {
  selectBarCodeRules.visible = true
}
function onSelectBarCodeRules(data) {
  formInfo.data = {
    ...formInfo.data,
    g1: data.g1,
    g1Value: data.value,
  }
  selectBarCodeRules.rules = data.g1
}
//获取厂家列表
function getBrandList() {
  getBrandAll().then((resp) => {
    if (resp && resp.success && resp.data && resp.data.length > 0) {
      listTypeInfo.brandList = resp.data.map(function (item) {
        return { key: item.name, value: item.id }
      })
    }
  })
}
//获取计量单位列表
function getUnitList() {
  console.log('获取计量单位列表')
  getUnitAll().then((resp) => {
    if (resp && resp.success && resp.data && resp.data.length > 0) {
      listTypeInfo.unitList = resp.data.map(function (item) {
        return { key: item.name, value: item.id }
      })
    }
  })
}
function getClassficationTree() {
  getProductCategoryTree().then((res) => {
    if (res && res.data && res.data.length > 0) {
      options.value = res.data.children
    }
  })
}
// 选择征照
function selectLicense(data) {
  proxy.$nextTick(function () {
    if (data) {
      formInfo.data = {
        ...formInfo.data,
        licenseNumber: data.licenseNumber,
        licenseTypeValue: data.licenseTypeValue,
        licenseName: data.licenseName,
        licenseBeginDate: data.licenseBeginDate,
        licenseEndDate: data.licenseEndDate,
        brandName: data.brandName,
        enName: data.enName,
        packedStandard: data.packedStandard,
        licenseId: data.id,
        commonName: data.licenseName,
        brandId: data.brand,
      }
      disabledFieldListItem(true, 'brandName')
      disabledFieldListItem(true, 'brandId')
    } else {
      formInfo.data = {
        ...formInfo.data,
        licenseNumber: '',
        licenseTypeValue: '',
        licenseName: '',
        licenseBeginDate: '',
        licenseEndDate: '',
        brandName: '',
        enName: '',
        packedStandard: '',
        licenseId: -2,
        commonName: '',
        brandId: '',
      }
      disabledFieldListItem(false, 'brandName')
      disabledFieldListItem(false, 'brandId')
    }
  })
}
//保存 覆盖
function save() {
  formInfo.ref.validate((valid) => {
    if (valid) {
      //验证--当单位换算系数不为1，计量单位和计算单位应不同
      if (
        formInfo.data.unitConvertCoefficient !== 1 &&
        formInfo.data.qtyOperateMode === 2
      ) {
        if (formInfo.data.unitId === formInfo.data.calcUnitId) {
          ElMessage.warning({
            message: '单位换算系数不为1时，计量单位与计算单位应不同，请修改！',
            duration: 0,
            showClose: true,
          })
          return
        }
      }
      let api = updateProductCheck
      const params = {
        ...formInfo.data,
        brandId: formInfo.data.brandId ? formInfo.data.brandId : -2,
        calcUnitId:
          formInfo.data.qtyOperateMode !== 2
            ? formInfo.data.unitId
            : formInfo.data.calcUnitId,
        unitConvertCoefficient:
          formInfo.data.qtyOperateMode == 2
            ? formInfo.data.unitConvertCoefficient
            : 1,
        type: 0,
      }
      params.classifyId = -1
      params.tag = tag.value
      if (tag.value == null || tag.value == undefined) {
        params.tag = 0
      }
      api(params)
        .then((res) => {
          if (res.success) {
            showDialog.value = false
            emit('returnBack', false)
          }
        })
        .catch(() => {})
    }
  })
}
//确认
function confirm() {
  tag.value = 1
  save()
}
// 审核
function audit() {
  const ids = [props.id]
  productCheckAudit(ids).then((resp) => {
    if (resp && resp.success) {
      showDialog.value = false
      emit('returnBack', false)
    }
  })
}
// 撤回
function revoke() {
  const ids = [props.id]
  productCheckRevoke(ids).then((resp) => {
    if (resp && resp.success) {
      showDialog.value = false
      emit('returnBack', false)
    }
  })
}
//拒绝
function refuse() {
  const ids = [props.id]
  productCheckRefuse(ids).then((resp) => {
    if (resp && resp.success) {
      showDialog.value = false
      emit('returnBack', false)
    }
  })
}
function getDetails() {
  console.log('查询产品信息')
  getProductCheck(props.id).then((resp) => {
    if (resp && resp.success) {
      formInfo.data = {
        ...resp.data,
        brandId: resp.data.brandId === -2 ? '' : resp.data.brandId,
      }

      getGSRule()
    }
  })
}
function stringFillCharacter(length) {
  return new Array(length + 1).join('x')
}
function getGSRule() {
  const id = props.id
  getCoreProductRule(id).then((res) => {
    if (res && res.success && JSON.stringify(res.data) !== '{}') {
      let params = {
        g1: res.data.coreProductRuleVOList,
      }

      if (res.data.type === 'GS1_OTHER') {
        const eg = []
        res.data.coreProductRuleVOList.map((item) => {
          eg.push(
            String(
              '(' + item.ruleCode + ')' + stringFillCharacter(item.ruleLength)
            )
          )
          return item
        })

        params = {
          ...params,
          value: eg.toString().replace(/,/g, ''),
        }
      } else {
        params = {
          ...params,
          value: 'GS1规则',
        }
      }
      formInfo.data = {
        ...formInfo.data,
        g1: res.data,
        g1Value: params.value,
      }
      selectBarCodeRules.rules = res.data
    }
  })
}
function getStatus() {
  getCoreCodeStatus({ codeType: '2' }).then((res) => {
    showFieldListItem(!res.data, 'code')
    formInfo.rules.code[1].required = !res.data
  })
}
function getCoreSystemConfig() {
  let param = {
    systemConfigCodeMult: 'qty_operate_mode,code_type_support',
  }
  let fieldList = formInfo.fieldList
  getCoreSystemConfigAll(param).then((res) => {
    if (res && res.success && res.data && res.data.length) {
      res.data.forEach((item) => {
        formInfo.systemConfig[item.systemConfigCode] = Number(item.value)
        if (item.systemConfigCode === 'qty_operate_mode') {
          showFieldListItem(
            (props.id ? formInfo.data.qtyOperateMode : item.value) == 2,
            'calcUnitId'
          )
          showFieldListItem(
            (props.id ? formInfo.data.qtyOperateMode : item.value) == 2,
            'unitConvertCoefficient'
          )
        } else if (item.systemConfigCode === 'code_type_support') {
          // let value =
          //   item.value.indexOf(",") != -1
          //     ? item.value.split(",")
          //     : [item.value];
          // fieldList.map(item => {
          //   if (item.value === "codeType") {
          //     item.onlyShowOption = value;
          //   }
          // });
          // console.log(fieldList)
        }
      })
    }
  })
}
</script>

<style scoped></style>
